﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-04 10:19:32
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	
	/// <summary>
	///菜单角色关联表 数据访问层
	/// </summary>	
	internal partial class AuthorityRoleMenuRela
	{		
    
        /// <summary>
    	/// 根据断言条件获取单个菜单角色关联表
    	/// </summary>
        public AuthorityRoleMenuRelaInfo QuerySingle(Expression<Func<AuthorityRoleMenuRelaInfo, bool>> predicate)
	    {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        /// <summary>
    	/// 根据断言条件获获取菜单角色关联表列表
    	/// </summary>
	    public IList<AuthorityRoleMenuRelaInfo> QueryList(Expression<Func<AuthorityRoleMenuRelaInfo, bool>> predicate)
	    {
	        return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
	    }

        /// <summary>
        /// 根据角色获取菜单 
        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="SystemID"></param>
        public IList<AuthorityMenuInfo> GetRoleMenuList(List<int> RoleIds, string SystemCode)
        {
            IList<AuthorityMenuInfo> list = new List<AuthorityMenuInfo>();
            if (RoleIds.Count == 0)
            {
                return list;
            }
            string intRoleId = string.Join(",", RoleIds.ToArray());
            string strSql = @" select * from [AuthorityMenu] with(nolock) where isactive=1 and menuid in(select MenuID from [AuthorityRoleMenuRela] a with(nolock) INNER JOIN [AuthoritySystem] b with(nolock)
                               ON a.SystemID=b.SystemID where b.SystemCode=@SystemCode and RoleID in(" + intRoleId + @") and a.IsActive=1 and b.IsActive=1 group by MenuID)
                               ";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@SystemCode", DbType.String, SystemCode);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    AuthorityMenuInfo info = SQLServerProvider.Create<AuthorityMenuInfo>(dr);
                    list.Add(info);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取权限
        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="SystemID"></param>
        public IList<AuthorityRoleMenuRelaInfo> GetRoleMenuBySystemIDandRoleID(int RoleID)
        {
            IList<AuthorityRoleMenuRelaInfo> list = new List<AuthorityRoleMenuRelaInfo>();
            string strSql = @" SELECT  *
                                  FROM [dbo].[AuthorityRoleMenuRela] with(nolock)
                                  WHERE [IsActive]=1 AND [RoleID]=@RoleID";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@RoleID", DbType.Int32, RoleID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    AuthorityRoleMenuRelaInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            return list;
        }

        /// <summary>
        /// 删除权限        
        /// </summary>
        /// <param name="RoleID"></param>
        public void DeleteRoleMenu(int RoleID)
        {
            string strSql = @"	  UPDATE [AuthorityRoleMenuRela] SET IsActive=0,UpdateTime=GETDATE()
                                  WHERE [RoleID]=@RoleID ";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            //设置参数
            db.AddInParameter(dbc, "@RoleID", DbType.Int32, RoleID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除父级角色以下所有权限配置
        /// </summary>
        /// <param name="pRoleId"></param>
        public void DeleteRoleMenuByParentRoleId(int pRoleId)
        {
            string strSql = @"	 update [AuthorityRoleMenuRela] SET IsActive=0,UpdateTime=GETDATE() where roleid=@RoleId or roleid in(select RoleID from AuthorityRole where ParentID=@RoleId)";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            //设置参数
            db.AddInParameter(dbc, "@RoleId", DbType.Int32, pRoleId);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
        }

    }
	
}